﻿package Interface
{	
	import Core.InterfacePage;	
	import Interface.TracksClasses.TracksScroller;
	import Interface.TracksClasses.TrackButton;
	import Interface.TracksClasses.TrackLabel;
	import GameUtils.TrackData;
	
	import flash.events.MouseEvent;	
	import flash.events.Event;
	import flash.text.TextField;	
	
	public class PageTracks extends InterfacePage
	{
		private const CAnotherTrackIsLoading:String = 'Пожалуйста дождитесь загрузки предыдущего трека';
		private const CTrackIsLoading:String = 'Пожалуйста подождите, этот трек сейчас загружается';
		
		private var FData:Vector.<TrackData>;
		private var FScroller:TracksScroller;
		private var FTrainingButton:TrackButton;
		private var FBattleButton:TrackButton;
		private var FCurrentData:TrackData;
		private var FInfoText:TextField;
		private var FLoadingCount:int;
		
		public function PageTracks():void
		{
			FLoadingCount = 0;
			FCurrentData = null;
			FScroller = getChildByName('TracksListSroller') as TracksScroller;
			FTrainingButton = getChildByName('TrainingButton') as TrackButton;
			FBattleButton = getChildByName('BattleButton') as TrackButton;
			FInfoText = getChildByName('TrackLodingText') as TextField;
			FInfoText.visible = false;
			FTrainingButton.Text = 'Тренировка';
			FBattleButton.Text = 'Создать битву';
			FTrainingButton.addEventListener(MouseEvent.MOUSE_DOWN, OnTrainingButton);
			FBattleButton.addEventListener(MouseEvent.MOUSE_DOWN, OnBattleButton);
			
			FTrainingButton.visible = false;
			FBattleButton.visible = false;
			FInfoText.visible = false;
		}
		
		public function set Data(AData:Object):void
		{			
			FData = Vector.<TrackData>(AData);
			FScroller.Data = FData;
			for(var i in FScroller.Items)
			{				
				FScroller.Items[i].addEventListener(MouseEvent.MOUSE_DOWN, OnLabelClick);
				FScroller.Items[i].Data.addEventListener(Event.COMPLETE, CheckButtons);
			}
		}
		
		private function OnLabelClick(e:MouseEvent):void
		{
			SelectLabel(e.target.parent);
		}
		
		private function SelectLabel(ALabel:TrackLabel):void
		{
			for(var i in FScroller.Items)
			{
				if(FScroller.Items[i] != ALabel)
					FScroller.Items[i].Selected = false
				else
				{
					FScroller.Items[i].Selected = true;
					with(FScroller.Items[i].Data)
					{
						if(!IsLoaded && !IsLoading)
						{
							if(FLoadingCount < 1)
							{
								FLoadingCount++;
								Load();
							}
							else
							{
								FInfoText.visible = true;
								FInfoText.text = CAnotherTrackIsLoading;
							}
						}
					}
					FCurrentData = FScroller.Items[i].Data;					
					
					CheckButtons();
				}
			}
		}
		
		private function CheckButtons(e:Event = null):void
		{
			if(e)
			{			
				FLoadingCount--;
				
				if(!FCurrentData.IsLoading && !FCurrentData.IsLoaded)
				{
					FLoadingCount++;
					FCurrentData.Load();
				}
			}
						
			FTrainingButton.visible = FCurrentData.IsLoaded;
			FBattleButton.visible = FCurrentData.IsLoaded;	
			FInfoText.visible = !FCurrentData.IsLoaded;
			
			if(FCurrentData.IsLoading)
				FInfoText.text = CTrackIsLoading;				
		}
		
		private function OnTrainingButton(e:MouseEvent):void
		{
			FGame.StartTraining(FCurrentData);
		}
		
		private function OnBattleButton(e:MouseEvent):void
		{
			FGame.CurrentPage = FGame.Battles;
			FGame.Battles.CreateBattle(FCurrentData);			
		}
		
		public function TrackByID(AID: int):TrackData
		{
			var vResult:TrackData = null;
			for(var i in FData)
			{
				if(FData[i].ID == AID)
				{
					vResult = FData[i];
					break;
				}
			}
			return vResult;
		}
		
		public function RandomTrack():TrackData
		{
			return FData[Math.round(Math.random() * (FData.length - 1))];
		}
		
		public function Load(ATrack:TrackData):void
		{
			for(var i in FScroller.Items)
			{
				if(FScroller.Items[i].Data == ATrack)
				{
					SelectLabel(FScroller.Items[i]);
					break;
				}
			}
		}
		
		public function get All():Vector.<TrackData>
		{
			return FData;
		}
	}	
}